export function hasOwnProp(obj: Record<string, any>, key: string) {
  return Object.prototype.hasOwnProperty.call(obj, key);
}

export function isObj(obj: Record<string, any>) {
  return Object.prototype.toString.call(obj) === "[object Object]";
}
// 递归合并对象，如果属性是对象，同样合并
export function mergeObj(
  obj1: Record<string, any>,
  obj2: Record<string, any>,
  strict = false
) {
  const newObj: Record<string, any> = {};
  for (let key in obj1) {
    newObj[key] = obj1[key];
  }
  for (const key in obj2) {
    if (!strict || hasOwnProp(newObj, key)) {
      if (isObj(newObj[key])) {
        newObj[key] = mergeObj(newObj[key], obj2[key], strict);
      } else {
        newObj[key] = obj2[key];
      }
    }
  }
  return newObj;
}

export const CLIENT = document.documentElement || document.body;
//屏幕宽度
export let CLIENT_WIDTH = CLIENT.clientWidth;
//屏幕高度
export let CLIENT_HEIGHT = CLIENT.clientHeight;
/** 刷新屏幕尺寸 */
export function reloadClient() {
  CLIENT_WIDTH = CLIENT.clientWidth;
  CLIENT_HEIGHT = CLIENT.clientHeight;
}

/** 简单的防抖 */
export function debounce(fn: Function, delay = 300) {
  let timer;
  return function (this: any, ...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

/** 首字母大写 */
export function firstUpCase(str: string){
  return str.slice(0,1).toUpperCase() +str.slice(1).toLowerCase();
}